<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/main.css" />
  <title>聊天机器人</title>
</head>

<body>
  <div class="wrap">
    <div class="header">
      <h3>小思同学</h3>
      <img src="img/person01.png" alt="icon" />
    </div>
    <div class="main">
      <ul class="talk_list" style="top: 0px;" id="talk_list">
        <li class="left_word">
          <img src="img/person01.png" /> <span>嗨，最近想我没有？</span>
        </li>
      </ul>
    </div>
    <div class="footer">
      <img src="img/person02.png" alt="icon" />
      <input type="text" placeholder="说的什么吧..." class="input_txt" id="ipt" />
      <input type="button" value="发 送" class="input_sub" id="btnSend" />
    </div>
  </div>
  <audio src="" id="voice" autoplay style="display: none;"></audio>
  <script type="text/javascript" src="./js/axios.js"></script>
  <script>
    const ipt = document.querySelector('#ipt')
    const btnSend = document.querySelector('#btnSend')
    const ul = document.querySelector('#talk_list')
    const voice = document.querySelector('#voice')
    axios.defaults.baseURL = 'http://www.liulongbin.top:3006'
    btnSend.addEventListener('click', async () => {
      const str = ipt.value
      if (str === '') {
        return
      }
      const li = document.createElement('li')
      li.className = 'right_word'
      li.innerHTML = `<img src="img/person02.png" /> <span>${str}</span>`
      ul.appendChild(li)
      ipt.value = ''
      ul.scrollTop = ul.scrollHeight
      const { data: res } = await axios.get('/api/robot', {
        params: {
          spoken: str
        }
      })
      const text = res.data.info.text
      const liw = document.createElement('li')
      liw.className = 'left_word'
      liw.innerHTML = `<img src="img/person01.png" /> <span>${text}</span>`
      ul.appendChild(liw)
      ul.scrollTop = ul.scrollHeight
      const { data: resv } = await axios.get('/api/synthesize', {
        params: {
          text
        }
      })
      voice.src = resv.voiceUrl
    })
    ipt.addEventListener('keyup', e => {
      if (e.key === 'Enter') {
        btnSend.click()
      }
    })
  </script>
</body>

</html>